package fr.utbm.VI51.lemmings_world_editor;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


/**
 * @author Belabbas Tarik - Hoffer Adrien - Perradin Vincent - Vinson Guillaume
 * 
 * This panel allow the user to select a brush with different buttons (one per brush)
 *
 */
public class BrushSelectorPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3710995991917066359L;
	
	private ButtonGroup buttonGroup;
	private JRadioButton earth;
	private JRadioButton rock;
	private JRadioButton water;
	private JRadioButton fire;
	private JRadioButton air;
	private JRadioButton entrance;
	private JRadioButton exit;
	
	/**
	 * The selected brush
	 */
	private Map.elements selectedBrush;
	
	public BrushSelectorPanel(String sTitle) {
		
		this.setSelectedBrush(Map.elements.EARTH);
		//Borders
		setLayout(new FlowLayout());
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		setBorder(border);
		setBorder(BorderFactory.createTitledBorder(border, sTitle, TitledBorder.CENTER, TitledBorder.TOP));
		
		
		this.buttonGroup = new ButtonGroup();
		
		//Create the different brushes checkBoxes
		this.earth = new JRadioButton("earth", true); //$NON-NLS-1$
		this.rock = new JRadioButton("rock", false); //$NON-NLS-1$
		this.water = new JRadioButton("water", false); //$NON-NLS-1$
		this.fire = new JRadioButton("fire", false); //$NON-NLS-1$
		this.air = new JRadioButton("air", false); //$NON-NLS-1$
		this.entrance = new JRadioButton("entrance", false); //$NON-NLS-1$
		this.exit = new JRadioButton("exit", false); //$NON-NLS-1$
		
		// Set the actionListeners
		this.earth.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.EARTH);
			}
		});
		
		this.rock.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.ROCK);
			}
		});
		
		this.water.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.WATER);
			}
		});
		
		this.fire.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.FIRE);
			}
		});
		
		this.air.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.AIR);
			}
		});
		
		this.entrance.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.ENTRANCE);
			}
		});
		
		this.exit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				BrushSelectorPanel.this.setSelectedBrush(Map.elements.EXIT);
			}
		});
		
		this.buttonGroup.add(this.earth);
		this.buttonGroup.add(this.rock);
		this.buttonGroup.add(this.water);
		this.buttonGroup.add(this.fire);
		this.buttonGroup.add(this.air);
		this.buttonGroup.add(this.entrance);
		this.buttonGroup.add(this.exit);
		
		this.add(this.earth);
		this.add(this.rock);
		this.add(this.water);
		this.add(this.fire);
		this.add(this.air);
		this.add(this.entrance);
		this.add(this.exit);
		
	}

	/**
	 * Set the selected brush
	 * 
	 * @param selectedBrush the selectedBrush to set
	 */
	public void setSelectedBrush(Map.elements selectedBrush) {
		this.selectedBrush = selectedBrush;
	}

	/**
	 * Returns the selected brush
	 * 
	 * @return the selectedBrush
	 */
	public Map.elements getSelectedBrush() {
		return this.selectedBrush;
	}

}
